package com.retirement.access.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retirement.access.domain.OldAccess;
import com.retirement.access.dto.AccessDto;
import com.retirement.access.dto.AccessPageDto;
import com.retirement.access.mapper.OldAccessMapper;
import com.retirement.access.service.IOldAccessService;
import com.retirement.access.vo.AccessPageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retirement.common.utils.bean.BeanUtils;
import com.retirement.information.domain.OldInformation;
import com.retirement.information.mapper.OldInformationMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2024-03-20
 */
@Service
public class OldAccessServiceImpl extends ServiceImpl<OldAccessMapper, OldAccess> implements IOldAccessService {

    @Resource
    OldInformationMapper oldInformationMapper;

    @Override
    public int addAccess(AccessDto dto) {
        OldInformation oldInformation = oldInformationMapper.selectOne(new LambdaQueryWrapper<OldInformation>()
                .eq(OldInformation::getIdentity,dto.getIdentity()));
        Assert.isTrue(ObjectUtil.isNotNull(oldInformation), "不存在该身份信息");
        OldAccess oldAccess = new OldAccess();
        oldAccess.setInformationId(oldInformation.getId());
        oldAccess.setId(IdUtil.simpleUUID());
        BeanUtils.copyProperties(dto,oldAccess);

        return baseMapper.insert(oldAccess);
    }

    @Override
    public List<AccessPageVo> getPage(AccessPageDto dto) {
        return baseMapper.getPage(dto);
    }

    @Override
    public AccessPageVo getDetail(String id) {
        OldAccess oldAccess = baseMapper.selectById(id);
        OldInformation oldInformation = oldInformationMapper.selectById(oldAccess.getInformationId());
        AccessPageVo vo = new AccessPageVo();
        vo.setName(oldInformation.getName());
        vo.setIdentity(oldInformation.getIdentity());
        vo.setVisits(oldAccess.getVisits());
        vo.setVisitsRecords(oldAccess.getVisitsRecords());

        return vo;
    }

    @Override
    public int updateAccess(OldAccess dto) {
        OldAccess oldAccess = baseMapper.selectById(dto.getId());
        Assert.isTrue(ObjectUtil.isNotNull(oldAccess), "不存在该走访信息");

        return baseMapper.updateById(dto);
    }

}